package com.cyt.web.controller.work.enrollment;

import com.cyt.base.enums.BusinessType;
import com.cyt.base.enums.OperatorType;
import com.cyt.base.model.dto.ReturnResult;
import com.cyt.sys.aspect.BaseLog;
import com.cyt.web.controller.base.BaseController;
import com.cyt.work.enrollment.model.po.EnrollmentRelease;
import com.cyt.work.enrollment.model.po.EnrollmentStuinfo;
import com.cyt.work.enrollment.model.vo.EnrollmentReleaseVo;
import com.cyt.work.enrollment.model.vo.EnrollmentStuinfoVo;
import com.cyt.work.enrollment.service.EnrollmentReleaseService;
import com.cyt.work.enrollment.service.EnrollmentStuinfoService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 自主招生-招生发布控制层
 *
 * @author caideyong
 * @date 2019-11-22 16:06:41
 */
@RestController
public class EnrollmentReleaseController extends BaseController {

    @Resource
    private EnrollmentReleaseService enrollmentReleaseService;
    @Autowired
    private EnrollmentStuinfoService enrollmentStuinfoService;
    /**
     * 统一跳转
     *
     * @param pageName 页面名称
     * @param applyId  应用id
     * @return ModelAndView
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @RequestMapping("/enrollment/enrollmentrelease/{pageName}")
    public ModelAndView goPage(@PathVariable("pageName") String pageName, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentrelease/" + pageName);
        view.addObject("applyId", applyId);
        return view;
    }

    /**
     * 分页查询
     *
     * @param enrollmentReleaseVo 接受参数对象
     * @param page                当前页面
     * @param limit               页面数量
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "分页查询对象", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/page", method = RequestMethod.POST)
    public ReturnResult page(EnrollmentReleaseVo enrollmentReleaseVo, int page, int limit) {
        return enrollmentReleaseService.page(enrollmentReleaseVo, page, limit);
    }

    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "跳转添加或编辑界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/getEdit", method = RequestMethod.GET)
    public ModelAndView getEdit(String id, String parentMenuId, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentrelease/enrollmentReleaseEdit");
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentRelease", enrollmentReleaseService.selectByPrimaryKey(id));
        }
        view.addObject("parentMenuId", parentMenuId);
        view.addObject("applyId", applyId);
        return view;
    }
    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "跳转章程添加界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/enrollmentReleaseRuleList", method = RequestMethod.GET)
    public ModelAndView getEdit(String id, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentrelease/enrollmentReleaseRuleList");
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentRelease", enrollmentReleaseService.selectByPrimaryKey(id));
        }
        view.addObject("applyId", applyId);
        return view;
    }


    /**
     * 跳转添加或编辑界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "跳转审核界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/getCheck", method = RequestMethod.GET)
    public ModelAndView getCheck(String id, String parentMenuId, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentstuinfo/enrollmentStuinfoList");
        view.addObject("releaseId",id);
        view.addObject("parentMenuId", parentMenuId);
        view.addObject("applyId", applyId);
        if(StringUtils.isNotBlank(id)){
            EnrollmentRelease enrollmentRelease=enrollmentReleaseService.selectByPrimaryKey(id);
            view.addObject("releaseState",enrollmentRelease.getState());
            view.addObject("enrollmentRelease",enrollmentRelease);
            //查询已通过的人数
            EnrollmentStuinfoVo enrollmentStuinfoVo=new EnrollmentStuinfoVo();
            enrollmentStuinfoVo.setReleaseId(id);
            enrollmentStuinfoVo.setReviewState("2");
            List<EnrollmentStuinfo> enrollmentStuinfoList=enrollmentStuinfoService.listExample(enrollmentStuinfoVo);
            if(enrollmentStuinfoList!=null){
                view.addObject("num",enrollmentStuinfoList.size());
            }
        }
        return view;
    }

    /**
     * 跳转详情界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "跳转详情界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/getDetail", method = RequestMethod.GET)
    public ModelAndView getDetail(String id, String parentMenuId, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentrelease/enrollmentReleaseDetail");
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentRelease", enrollmentReleaseService.selectByPrimaryKey(id));
        }
        view.addObject("parentMenuId", parentMenuId);
        view.addObject("applyId", applyId);
        return view;
    }

    /**
     * 跳转二维码界面
     *
     * @param id 主键
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "跳转二维码界面", businessType = BusinessType.GET, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/getCode", method = RequestMethod.GET)
    public ModelAndView getCode(String id, String parentMenuId, String applyId) {
        ModelAndView view = new ModelAndView("/work/enrollment/enrollmentrelease/enrollmentReleaseCode");
        if (StringUtils.isNotBlank(id)) {
            view.addObject("enrollmentRelease", enrollmentReleaseService.selectByPrimaryKey(id));
        }
        view.addObject("parentMenuId", parentMenuId);
        view.addObject("applyId", applyId);
        return view;
    }

    /**
     * 保存或更新
     *
     * @param request             请求对象
     * @param enrollmentReleaseVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "保存或更新对象", businessType = BusinessType.INSERT, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/addOrUpdate", method = RequestMethod.POST)
    public ReturnResult addOrUpdate(HttpServletRequest request, EnrollmentReleaseVo enrollmentReleaseVo) {
        return enrollmentReleaseService.addOrUpdate(request,enrollmentReleaseVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件逻辑删除
     *
     * @param request             请求对象
     * @param enrollmentReleaseVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "逻辑删除对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/remove", method = RequestMethod.GET)
    public ReturnResult remove(HttpServletRequest request, EnrollmentReleaseVo enrollmentReleaseVo) {
        return enrollmentReleaseService.remove(enrollmentReleaseVo, acceptCurrentUser(request));
    }

    /**
     * 根据条件物理删除
     *
     * @param request             请求对象
     * @param enrollmentReleaseVo 接受参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019-11-22 16:06:41
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "物理对象对象", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/delete", method = RequestMethod.GET)
    public ReturnResult delete(HttpServletRequest request, EnrollmentReleaseVo enrollmentReleaseVo) {
        return enrollmentReleaseService.delete(enrollmentReleaseVo, acceptCurrentUser(request));
    }

    /**
     * 结束发布
     *
     * @param enrollmentReleaseVo 传入参数对象
     * @return ReturnResult
     * @author caideyong
     * @date 2019/12/3
     */
    @BaseLog(module = "自主招生", classz = "招生发布", methods = "结束发布", businessType = BusinessType.DELETE, operatorType = OperatorType.MANAGE)
    @RequestMapping(value = "/enrollment/enrollmentrelease/endRelease", method = RequestMethod.GET)
    public ReturnResult endRelease(HttpServletRequest request, EnrollmentReleaseVo enrollmentReleaseVo) {
        return enrollmentReleaseService.endRelease(enrollmentReleaseVo, acceptCurrentUser(request));
    }
}